package com.mall.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mall.common.utils.Response;
import com.mall.common.utils.ResponseMessage;
import com.mall.dao.SpuInfoDao;
import com.mall.vo.Attr;
import com.mall.vo.PurchaseVo;
import com.mall.vo.SpuInfoVo;
import com.mall.vo.SpuSaveVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.mall.entity.SpuInfo;
import com.mall.service.SpuInfoService;
import com.mall.common.utils.PageUtils;
import com.mall.common.utils.R;

import javax.servlet.http.HttpServletRequest;


@RestController
@CrossOrigin
@RequestMapping("product/spuinfo")
public class SpuInfoController {
    @Autowired
    private SpuInfoService spuInfoService;






    @GetMapping("/purchase/{spuId}")
    public Response purchaseBuy(@PathVariable("spuId")Long spuId){
        List<Attr> purchaseBuy = spuInfoService.getPurchaseBuy(spuId);
        return new Response(ResponseMessage.SUCCESS).data(purchaseBuy);
    }





    /**
     * 搜索，按照最新时间排序，按照销量排序，按照价格排序
     * @return
     */
    @GetMapping("/search/{currentPage}/{pageSize}")
    public Response selectAllPage(@PathVariable("currentPage") Integer currentPage,@PathVariable("pageSize")Integer pageSize,HttpServletRequest request){
        String spuName = request.getParameter("spuName");
//        Integer cur=Integer.valueOf(currentPage);
//        Integer page=Integer.valueOf(pageSize);
//        System.out.println(currentPage);
        IPage<SpuInfo> iPage = spuInfoService.listByNamePage(currentPage, pageSize, spuName);

        return new Response(ResponseMessage.SUCCESS).data(iPage);
    }


    /**
     * @param
     * @return
     */
    @PostMapping("/list")
    public Response selectByDateAndPriceAndSales(@RequestBody Map<String, String> map){
        Integer currentPage=0;
        Integer pageSize=0;
        if (map.containsKey("currentPage")){
            currentPage =Integer.valueOf(map.get("currentPage"));
        }
        if (map.containsKey("pageSize")){
            pageSize =Integer.valueOf(map.get("pageSize"));
        }

//        Integer pageSize = Integer.valueOf(request.getParameter("pageSize"));
        String spuName="";
        if (map.containsKey("spuName")){
            spuName = map.get("spuName");
        }

//        System.out.println(spuName);


        Integer isUpSale=-1;
        Integer prices= -1;
        Integer isUpNew=-1;

        if (map.containsKey("isUpSale")){
            isUpSale = Integer.valueOf(map.get("isUpSale"));
        }
        if (map.containsKey("prices")){
            isUpSale = Integer.valueOf(map.get("prices"));
        }
        if (map.containsKey("isUpNew")){
            isUpSale = Integer.valueOf(map.get("isUpNew"));
        }

        IPage<SpuInfo> iPage=null;
        Response response;
        if (isUpSale == 1){
            iPage = spuInfoService.listBySales(currentPage, pageSize, spuName);
            response = new Response(ResponseMessage.SUCCESS).data(iPage);
            return response;
        }
        if (isUpNew == 1){
            iPage = spuInfoService.listByDate(currentPage, pageSize, spuName);
            response = new Response(ResponseMessage.SUCCESS).data(iPage);
            return response;
        }
        if (prices == 0){
            iPage = spuInfoService.listByDesc(currentPage, pageSize, spuName);
            response = new Response(ResponseMessage.SUCCESS).data(iPage);
            return response;
        }
        if(prices == 1){
            iPage = spuInfoService.listByAsc(currentPage, pageSize, spuName);
            response = new Response(ResponseMessage.SUCCESS).data(iPage);
            return response;
        }
        response = new Response(ResponseMessage.FAIL);
        return response;
    }



    @GetMapping("/date/{currentPage}/{pageSize}")
    public Response selectByDate(@PathVariable("currentPage") Integer currentPage, @PathVariable("pageSize") Integer pageSize, HttpServletRequest request){
        String spuName = request.getParameter("spuName");
        if (spuName==null){
            spuName = "";
        }
        IPage<SpuInfo> iPage = spuInfoService.listByDate(currentPage, pageSize, spuName);
        return new Response(ResponseMessage.SUCCESS).data(iPage);
    }


    /**
     * flag=1 价格从小到大
     */
    @GetMapping("/price/{currentPage}/{pageSize}/{flag}")
    public Response selectByPrice(@PathVariable Integer currentPage,@PathVariable Integer pageSize,@PathVariable Integer flag,HttpServletRequest request){
        String spuName = request.getParameter("spuName");
        if (spuName==null){
            spuName = "";
        }
        IPage<SpuInfo> iPage;
        if (flag==0){
            iPage = spuInfoService.listByDesc(currentPage, pageSize, spuName);
        }else {
            iPage = spuInfoService.listByAsc(currentPage, pageSize, spuName);
        }
        return new Response(ResponseMessage.SUCCESS).data(iPage);
    }

    @GetMapping("/sales/{currentPage}/{pageSize}")
    public Response selectBySales(@PathVariable Integer currentPage,@PathVariable Integer pageSize,HttpServletRequest request){
        String spuName = request.getParameter("spuName");
        if (spuName==null){
            spuName = "";
        }
        IPage<SpuInfo> iPage = spuInfoService.listBySales(currentPage, pageSize, spuName);
        return new Response(ResponseMessage.SUCCESS).data(iPage);
    }



    

    /**
     * 列表
     */
    @RequestMapping("/listAll")
   /// @RequiresPermissions("product:spuinfo:list")
    public R listAll(){
//        PageUtils page = spuInfoService.queryPage(params);
        List<SpuInfo> list = spuInfoService.list(null);
        return R.ok().put("page", list);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //  @RequiresPermissions("product:spuinfo:info")
    public R info(@PathVariable("id") Long id){
        SpuInfo spuInfo = spuInfoService.getById(id);

        return R.ok().put("spuInfo", spuInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("product:spuinfo:save")
    public R save(@RequestBody SpuSaveVo vo){
        //spuInfoService.save(spuInfo);
        spuInfoService.saveSpuInfo(vo);
        return R.ok();
    }

    @RequestMapping("/save/info")
    public R saveinfo(@RequestBody SpuInfoVo vo){
        //spuInfoService.save(spuInfo);
//        spuInfoService.saveSpuInfo(vo);
//        System.out.println(vo);
        spuInfoService.saveInfo(vo);
        return R.ok();
    }



    /**
     * 修改
     */
    @RequestMapping("/update")
    //  @RequiresPermissions("product:spuinfo:update")
    public R update(@RequestBody SpuInfo spuInfo){
        spuInfoService.updateById(spuInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //   @RequiresPermissions("product:spuinfo:delete")
    public R delete(@RequestBody Long[] ids){
        spuInfoService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }







}
